package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.service.*;
import com.hushao.lims.weblims.utils.UserUtil;
import com.hushao.lims.weblims.vo.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author 930588706
 * @date 18/1/28
 * 审核
 */
@Controller
@RequestMapping("/audit")
public class AuditController {
    @Autowired
    private ConsumableStorageService consumableStorageService;

    @Autowired
    private EquipmentStorageService equipmentStorageService;

    @Autowired
    private EquipmentScrapService equipmentScrapService;

    @Autowired
    private EquipmentApplyService equipmentApplyService;

    @Autowired
    private ConsumableApplyService consumableApplyService;

    @Autowired
    private EquipmentRepairService equipmentRepairService;
    @Autowired
    private UserUtil userUtil;


    /**
     * 审核耗材入库
     * @param consumableStorageId
     * @param state
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @PostMapping("/consumableStorage/{consumableStorageId}")
    public ResponseData consumableStorage(@PathVariable("consumableStorageId") Long consumableStorageId,
                                          @RequestParam("state") String state) throws Exception{
        //获取当前登录者的信息
        TeacherInfo teacher= userUtil.isLogged(SecurityContextHolder.getContext().getAuthentication());

        consumableStorageService.audit(consumableStorageId,state,teacher);
        String message;
        switch (state){
            case "1":message="你通过了此耗材入库";break;
            case "2":message="你拒绝了此耗材入库";break;
            default:throw new Exception("无法识别你选择的操作！");
        }
        return new ResponseData(true,message);
    }

    /**
     * 审核器材入库
     * @param equipmentStorageId
     * @param state
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @PostMapping("/equipmentStorage/{equipmentStorageId}")
    public ResponseData equipmentStorage(@PathVariable("equipmentStorageId") Long equipmentStorageId,
                                         @RequestParam("state") String state) throws Exception{
        //获取当前登录者的信息
        TeacherInfo teacher= userUtil.isLogged(SecurityContextHolder.getContext().getAuthentication());
        equipmentStorageService.audit(equipmentStorageId,state,teacher);
        String message;
        switch (state){
            case "1":message="你通过了此器材入库";break;
            case "2":message="你拒绝了此器材入库";break;
            default:throw new Exception("无法识别你选择的操作！");
        }
        return new ResponseData(true,message);
    }

    /**
     * 审核器材报修
     * @param repairId
     * @param state
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @PostMapping("/equipmentRepair/{repairId}")
    public ResponseData equipmentRepair(@PathVariable("repairId") int repairId,
                                        @RequestParam("state") String state) throws Exception{
        //获取当前登录者信息
        TeacherInfo teacher= userUtil.isLogged(SecurityContextHolder.getContext().getAuthentication());
        equipmentRepairService.audit(repairId,state,teacher);
        String message;
        switch (state){
            case "1":message="你通过了此器材报修";break;
            case "2":message="你拒绝了此器材报修";break;
            default:throw new Exception("无法识别你选择的操作！");
        }
        return new ResponseData(true,message);
    }


    /**
     * 审核器材报废
     * @param equipmentScrapId
     * @param state
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @PostMapping("/equipmentScrap/{equipmentScrapId}")
    public ResponseData equipmentScrap(@PathVariable("equipmentScrapId") Long equipmentScrapId,
                                       @RequestParam("state") String state) throws Exception{
        //获取当前登录者的信息
        TeacherInfo teacher= userUtil.isLogged(SecurityContextHolder.getContext().getAuthentication());
        equipmentScrapService.audit(equipmentScrapId,state,teacher);
        String message;
        switch (state){
            case "1":message="你通过了此器材报废";break;
            case "2":message="你拒绝了此器材报废";break;
            default:throw new Exception("无法识别你选择的操作！");
        }
        return new ResponseData(true,message);
    }

    /**
     * 审核使用器材
     * @param equipmentApplyId
     * @param state
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @PostMapping("/equipmentApply/{equipmentApplyId}")
    public ResponseData equipmentApply(@PathVariable("equipmentApplyId") Long equipmentApplyId,
                                       @RequestParam("state") String state) throws Exception{
        TeacherInfo teacher= userUtil.isLogged(SecurityContextHolder.getContext().getAuthentication());
        equipmentApplyService.audit(equipmentApplyId,state,teacher);
        String message;
        switch (state){
            case "1":message="你通过了此器材申请";break;
            case "2":message="你拒绝了此器材申请";break;
            default:throw new Exception("无法识别你选择的操作！");
        }
        return new ResponseData(true,message);
    }

    /**
     * 审核使用耗材
     * @param consumableApplyId
     * @param state
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @PostMapping("/consumableApply/{consumableApplyId}")
    public ResponseData consumableApply(@PathVariable("consumableApplyId") Long consumableApplyId,
                                        @RequestParam("state") String state) throws Exception{
        TeacherInfo teacher= userUtil.isLogged(SecurityContextHolder.getContext().getAuthentication());
        consumableApplyService.audit(consumableApplyId,state,teacher);
        String message;
        switch (state){
            case "1":message="你通过了此耗材申请";break;
            case "2":message="你拒绝了此耗材申请";break;
            default:throw new Exception("无法识别你选择的操作！");
        }
        return new ResponseData(true,message);
    }
}
